package com.zoe.app.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author Zoe.Zhang
 * @date 2021/11/10
 * @description
 */
@RestController
public class OrderController {
    private static final String ORDER = "ORDER_";
    public static final String ORDER_SET = "ORDER_SET";

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/order")
    public String order() {
        long id = IdUtil.getSnowflake().nextId();
        String ids = ORDER + id;
        redisTemplate.opsForValue().set(ids, "001", 10, TimeUnit.SECONDS);
        return ids;
    }

    // http://localhost:8080/orderZset
    @GetMapping("/orderZset")
    public String orderZset() {
        // 一次性创建5个订单, 有效时间是 1 分钟
        for (int i = 0; i < 5; i++) {
            long id = IdUtil.getSnowflake().nextId();
            long expTime = DateUtil.current() + 1 * 60 * 1000;
            redisTemplate.opsForZSet().add(ORDER_SET, id, expTime);
        }
        return "ok";
    }


}
